#!/bin/bash
[ $# == 0 ] && input="$HOME/findroad/dat/blk/blk_*" || input=$1
table_format="(id mediumint not null, fnode mediumint, tnode mediumint, len float, district varchar(18), rname varchar(34), lon decimal(9,6), lat decimal(9,6), isnode tinyint , mblkid mediumint, index mblkid (mblkid), index lon_lat (lon,lat))"

for file in $(ls $input)
do
	table_name=`echo ${file##*/} |awk -F. '{print $1}'`
	SQL_drop="drop table $table_name;"
	SQL_create="create table $table_name ${table_format};"
	#SQL_csv_in="load data local infile '$file' into table $table_name fields terminated by ',' enclosed by '\"' lines terminated by '\\n' (id, fnode, tnode, len, rname, lon, lat);"
	SQL_csv_in="load data local infile '$file' into table $table_name fields terminated by ',' lines terminated by '\\n' (id, fnode, tnode, len, district, rname, lon, lat, isnode, mblkid);"
	#SQL="${SQL_drop}${SQL_create}${SQL_csv_in}"
	SQL="${SQL_create}${SQL_csv_in}" #1st time import.

	#echo
	#echo "table_name: $table_name"
	#echo "SQL_bdr: ${SQL_bdr}"
	#echo "SQL_drop: ${SQL_drop}"
	#echo "table_format: ${table_format}"
	#echo "SQL_create: ${SQL_create}"
	#echo "SQL_csv_in: ${SQL_csv_in}"
	#echo "SQL: $SQL"
	#echo
	echo -n "Importing table: $table_name ..."
	mysql -ugps -pgps -D road -e "$SQL"
	[ $? == 0 ] || exit 99
	echo "...Done"
done

#backup:mysql -ugps -pgps --skip-lock-tables road > bakfile
